//
//  AppendPostponeViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2025/3/11.
//

#import "AppendPostponeViewController.h"
#import "AppendPostponeModel.h"
#import "AppendPostponeTableViewCell.h"

@interface AppendPostponeViewController ()
@property (nonatomic ,strong) NSMutableArray< AppendPostponeModel *> * modelArray;
@end

@implementation AppendPostponeViewController
-(NSMutableArray<AppendPostponeModel *> *)modelArray {
    if(!_modelArray){
        _modelArray = [NSMutableArray array];
    }
    return  _modelArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(170));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:self.pondName andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.offset(0);
            make.top.equalTo(topview.mas_bottom);
    }];
    
    [self loadNewData];
    [self addHeaderView:^{
        [weakSelf loadNewData];
    }];
    [self addFooterView:^{
        [weakSelf loadNewData];
    }];
}

- (void)loadNewData{
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.pondId forKey:@"id"];
    [SCNetManger PostRecordListWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray * array = [NSArray yy_modelArrayWithClass:AppendPostponeModel.class json:model.result];
            if(array.count == self.modelArray.count){
                [self.modelArray removeAllObjects];
                [self.modelArray addObjectsFromArray:array];
                [self.tableview.mj_header endRefreshing];
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.modelArray addObjectsFromArray:array];
                [self.tableview.mj_footer endRefreshing];
                [self.tableview.mj_header endRefreshing];
            }
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.modelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.modelArray[indexPath.row].cellH;
    
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    AppendPostponeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AppendPostponeTableViewCell"];
    if (cell == nil) {
        cell = [[AppendPostponeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AppendPostponeTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.modelArray[indexPath.row];
    return cell ;
    
}

@end
